package com.ll.daipao.handler;

import com.ll.daipao.wechat.service.CoreService;
import com.ll.daipao.wechat.vo.City;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by rockyll on 2017/10/12.
 */
public class UrlInterceptorHandler implements HandlerInterceptor {

    private Logger logger = LoggerFactory.getLogger(UrlInterceptorHandler.class);
    @Autowired
    protected CoreService coreService;


    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String url = httpServletRequest.getRequestURI();
        logger.info("----当前url：{}--- 当前参数：{}",url,httpServletRequest.getQueryString());
        String wx_openid = (String) httpServletRequest.getSession().getAttribute(Constants.WX_OPENID);
        List<City> cities= (List<City>) httpServletRequest.getSession().getAttribute(Constants.WX_SCHOOL_INFO);
        if (cities==null||cities.size()<1) {
            cities = coreService.allSchool();
            httpServletRequest.getSession().setAttribute(Constants.WX_SCHOOL_INFO,cities);
        }
        httpServletRequest.getSession().setAttribute(Constants.WX_OPENID,"12");
//        if(StringUtils.isBlank(wx_openid)){
//            httpServletResponse.sendRedirect("http://t1.lbd99.com/daipao/sigin");
//            return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
